#!/bin/bash
#Author: YangYi
#Date: 2023-12-06
#Version: v1

#Author: YangYi
#Date: 2024-01-02
#Version: v2
#Add MonitorAlter

#Author: YangYi
#Date: 2024-01-17
#Version: v3
#Modify PROMETHEUS_FILE

#UpDate: 2024-04-11
#Version: v4
#Content: 增加ip对应集群名判断

#自定义过滤条件
FILTER="filebeat\|pause\|prometheus-k8s-0\|monitoring\|prometheus\|file-beat\|dns"

#临时文件路径
SAVE_PATH=/app/paasadmin/pids_prometheus.text

#告警伐值
ALERT_VALUE="100"

#格式化时间
TIME=`date +%F-%H:%M:%S`

#设置要保存的文件个数
SAVE_COUNT="48"

#文件过滤路径
FILTER_FILE=/app/paasadmin/ServerPidPrometheus/
sudo /bin/mkdir -p  $FILTER_FILE && chown -R paasadmin:paasadmin $FILTER_FILE

#存储路径
FILE_PATH=$FILTER_FILE$TIME-pid.txt

#米采集文件
PROMETHEUS_FILE="/home/patrol/prometheus_containers_apps_current.prom"

#普米指标格式化
TITLE="
# HELP prometheus_containers_apps_current_total Total number of WAL corruptions.\n# TYPE prometheus_container_app_current_total gauge
"

# 获取网卡ip
network=`/sbin/ip a s  | grep -iw "eth0\|bond0" | grep -i "10.145." | awk '{print $2}' | awk -F "/" '{print $1}' | head -1`
  
#初始化集群信息
network_type=""

#Ip对应集群名判断
function MatchCluster(){
# 使用 case 语句和条件表达式来判断 IP 地址区间  
case $network in  
    # 第一个区间 
    10.145.167.*)  
        network_type="paas-crm-container-c2"  
        ;;  
    # 第二个区间 
    10.145.169.[3-8])  
        network_type="paas-crm-container-c3"  
        ;;  
    # 第三个区间 
    10.145.158.7[7-9])  
        network_type="paas-crm-container-c4"  
        ;;  
    10.145.158.8[0-2])  
        network_type="paas-crm-container-c4"  
        ;;  
    10.145.158.13[7-9])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.14[0-9])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.15[0-4])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.169)  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.151.*)  
        network_type="acct-ccse-c1"  
        ;;  
    10.145.149.*)  
        network_type="mss-ccse-001"  
        ;;  
    10.145.166.1[0-7])  
        network_type="oss-ccse-01"  
        ;;  
    10.145.166.2[6-9])  
        network_type="oss-ccse-02"  
        ;;  
    10.145.166.3[0-6])  
        network_type="oss-ccse-02"  
        ;;  
    10.145.166.4[2-9])  
        network_type="oss-ccse-03"  
        ;;  
    10.145.166.5[0-7])  
        network_type="oss-ccse-03"  
        ;;  
    10.145.166.8[5-9])  
        network_type="oss-ccse-04"  
        ;;  
    10.145.166.9[0-2])  
        network_type="oss-ccse-04"  
        ;;  
    10.9.6.5[3-9])  
        network_type="oss-ccse-05"  
        ;;  
    10.9.6.6[0-3])  
        network_type="oss-ccse-05"  
        ;;  
    10.145.166.6[5-9])  
        network_type="public-ccse-c1"  
        ;;  
    10.145.166.7[0-6])  
        network_type="public-ccse-c1"  
        ;;  
    10.9.6.[6-20])  
        network_type="public-ccse-c2"  
        ;;  
    10.145.175.*)  
        network_type="paas-ccse-ipsdn"  
        ;;  
    # 如果 IP 不在任何区间内  
    *)  
        network_type="Unknown Network"  
        ;;  
  esac  
}


#节点pid统计
NODEPIDSALL=`cd /sys/fs/cgroup/pids/kubepods.slice && find -iname "pids.*" | awk -F "/" '{if ($4=="pids.current")print }' | xargs -I file sh -c 'echo -e file" \c" && cat file'|awk '$2 > 1' | grep "burstable\|besteffort" | awk '{print $2"/"$1}' | awk -F "/" '{print $1""$4}' | awk -F "kubepods-burstable-pod|kubepods-besteffort-pod" '{print $1" "$2}' | awk -F "." '{print $1}' | awk -F "_" '{print $1}' | grep -v "docker" | echo " "$network >> $SAVE_PATH | echo " "$network_type >> $SAVE_PATH | sort -nr  > $SAVE_PATH`

#阈值告警
function AlertService(){
  cat $SAVE_PATH | while read results
    do
      result=`echo $results | awk '{print $2}'`
      docker ps  | grep $result | grep -v $FILTER | head -1 | awk '{print $NF}' | awk -F "_" '{printf $3","$4}' >> $FILE_PATH && docker ps  | grep $result | grep -v $FILTER | head -1 | awk '{print $2}' | awk -F "/" '{printf ","$3 }' >> $FILE_PATH  && cat $SAVE_PATH | grep $result | awk '{printf ","$1}' >> $FILE_PATH  && echo -e "\r" >> $FILE_PATH
    done
  AlterFile=`ls -l | awk '{print $9}' | tail -1`

  echo -e $TITLE > $PROMETHEUS_FILE
  
  cat $AlterFile | awk -F "," '{ if ($1 != "" && $2 != "" && $3 != "") print "prometheus_containers_apps_current_total{namespace=\""$2"\",deploy=\""$3"\",pods=\""$1"\",,pods=\""$1"\"=\""$1"\"} "$4 }' >> $PROMETHEUS_FILE

  dos2unix $PROMETHEUS_FILE > /dev/null 2>&1 
}


#文件清理
function File_Cleanup(){

cd $FILTER_FILE
#1 判断文件个数
count=$(ls -l | wc -l)
delcount=$[$count - $SAVE_COUNT]
#2 是否进行删除
if [ $delcount -gt 0 ] ; then
        #获取时间最久的文件夹名称
        filesname=$(ls -tr | head -n $delcount)
        filesname=${filesname// /}
        filesarr=($filesname)
        #循环依次删除时间久的文件
        for each in ${filesarr[*]}
          do
          sudo rm -rf $each
          done
fi
}

MatchCluster
File_Cleanup
AlertService

